package patrol.mobile.com.mobilepatrol.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.adapter.MissionRegisterAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.OtherOfficeDetailBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

public class MissionRegisterActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.tv_start)
    TextView tvStart;
    private MissionRegisterAdapter mAdapter;
    private String currentTime = "";
    private List<OtherOfficeDetailBean> evaluationDetailBeans;
    private String orgId = "";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_examination_all;
    }

    @Override
    public void initView() {
        super.initView();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommonUtil.exitActivityAndBackAnim(MissionRegisterActivity.this, true);
                    }
                });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("任务登记");
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageResource(R.mipmap.add_white);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        tvStart.setVisibility(View.GONE);
        mySpinner01.setVisibility(View.GONE);
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MissionRegisterActivity.this, MissionActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mAdapter = new MissionRegisterAdapter(this);
        recyclerView.setAdapter(mAdapter);
        requestData();
    }


    private void requestData() {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GETUSERTASKLIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("reportorId", UserManager.getUserId());
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser parser = new JsonParser();
                JsonObject asJsonObject = parser.parse(response).getAsJsonObject();
                JsonArray list = asJsonObject.getAsJsonArray("list");
                evaluationDetailBeans = GsonUtil.jsonToList(list.toString(), OtherOfficeDetailBean.class);
                mAdapter.updateItems(evaluationDetailBeans);
            }
        });
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MissionRegisterActivity.this, MissionActivity.class);
                intent.putExtra("id", mAdapter.getData().get(position).getId());
                startActivity(intent);
            }
        });
    }
}
